window.onload = function () {

    // top5小区价格分布
    function initCharts (data){
        $.ajax({
            type: "POST",
            url: "/analyze/top5CommunityPriceTrend",
            data:data,
            success: function (msg){
                 myChart = echarts.init($('#communityPriceTrend').get(0));

                var allData = msg.result,
                    communitynamelist = [],
                    price = []
                    startDate = [];
                allData.forEach(function(data,index){
                    communitynamelist[index] = data.communityname
                    var trendPriceList = data.trendPriceCommunity.split(";")
                    price[index] = trendPriceList[0].split(",")
                    startDate = trendPriceList[1].split(",")
                });


              var  option = {
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross',
                            crossStyle: {
                                color: '#999'
                            }
                        },
                        formatter:'{b0}<br/>{a0}：{c0} 元</br>{a1}：{c1} 元</br>{a2}：{c2} 元<br/>{a3}：{c3} 元<br/>{a4}：{c4} 元<br/>'
                        // formatter: function(params) {
                        //     var res = '<div>';
                        //     res += '<strong>' + params[0].name + '</strong>'
                        //     for (var i = 0, l = params.length; i < l; i++) {
                        //         if (params[i].seriesName == '华龙苑南里') {
                        //             res += '<br/>' + params[i].seriesName + ' : ' + params[i].value + ' 笔'
                        //         }
                        //         if (params[i].seriesName == '贷款金额') {
                        //             res += '<br/>' + params[i].seriesName + ' : ' + params[i].value + ' 亿元'
                        //         }
                        //         if (params[i].seriesName == '贷款余额') {
                        //             res += '<br/>' + params[i].seriesName + ' : ' + params[i].value + ' 亿元'
                        //         }
                        //     }
                        //     res += '</div>';
                        //     return res;
                        // }
                    },
                    legend: {
                        selectedMode:false,
                        data: communitynamelist
                    },
                    xAxis: [
                        {
                            type: 'category',
                            axisLabel: {
                                interval: 0,
                                rotate: 0
                            },
                            data: startDate,
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            name: '价格（元）',
                            axisLabel: {
                                formatter: '{value}'
                            }
                        }
                    ],
                    series: [
                    ]
                };

                if(startDate.length>10){
                    option.xAxis[0].axisLabel.rotate = 30
                }

                // 渲染小区价格
                for (var i = 0; i < allData.length; i++) {
                    option.series[i] = {
                        name: communitynamelist[i],
                        type:'line',
                        symbol:'triangle',
                        symbolSize: 8,
                        data: price[i]
                    }
                }

                myChart.setOption(option);
                // window.onresize = function () {
                //     myChart.resize()
                //     myChart1.resize()
                // };
            }
        })
    };

    // 押品押入期分布
    function initCharts1(data){
        $.ajax({
            type: "POST",
            url: "/analyze/estateStartAnalyze",
            data:data,
            success: function (msg){
                 myChart1 = echarts.init($('#estateStart').get(0));
                if (msg.y1)
                {
                    $("#y1").text(msg.y1 + '年、');
                }else{
                    $("#y1").html('');
                }
                if (msg.y2)
                {
                    $("#y2").text(msg.y2 + '年、');
                }else{
                    $("#y2").html(''); 
                }
                if (msg.y3)
                {
                    $("#y3").text(msg.y3 + '年、');   
                }else{
                    $("#y3").html(''); 
                }

                var allData = msg.result,
                    quantity1 = [],
                    quantity2 = [],
                    startDate = [];
                allData.forEach(function(data,index){
                    quantity1[index] = data.counts;
                    quantity2[index] = data.percent;
                    startDate[index] = (data.startDate);
                });
              var option = {
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross',
                            crossStyle: {
                                color: '#999'
                            }
                        },
                        formatter: function(params) {
                            var res = '<div>';
                            res += '<strong>' + params[0].name + '</strong>'
                            for (var i = 0, l = params.length; i < l; i++) {
                                if (params[i].seriesName == '押品数量') {
                                    res += '<br/>' + params[i].seriesName + ' : ' + params[i].value + ' 笔'
                                }
                                if (params[i].seriesName == '押品占比') {
                                    res += '<br/>' + params[i].seriesName + ' : ' + (params[i].value * 100).toFixed(2) + '%'
                                }
                            }
                            res += '</div>';
                            return res;
                        }
                    },
                    legend: {
                        selectedMode:false,
                        orient: 'vertical',
                        bottom:'bottom',
                        data:['押品数量', '押品占比']
                    },
                    xAxis: [
                        {
                            type: 'category',
                            axisLabel: {
                                interval: 0,
                                rotate: 0
                            },
                            data: startDate
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            name: '数量(个)',
                            axisLabel: {
                                formatter: '{value}'
                            }
                        },
                        {
                            type: 'value',
                            name: '百分比',
                            axisLabel: {
                                formatter: '{value}'
                            }
                        }
                    ],
                    series: [
                        {
                            name:'押品数量',
                            type:'bar',
                            data:quantity1
                        },
                        {
                            name:'押品占比',
                            type:'bar',
                            yAxisIndex: 1,
                            data:quantity2
                        }
                    ]
                };
                if(startDate.length>10){
                    option.xAxis[0].axisLabel.rotate = 30
                }

                myChart1.setOption(option);
                window.onresize = function () {
                    myChart.resize()
                    myChart1.resize()
                };
            }
        })
    };

    var city = $('#city').val();
    var inputData = {
        city:city,
    };
    initCharts(inputData);
    initCharts1(inputData);

    $('#searchData').click(function() {
        initCharts(inputData);
        initCharts1(inputData);
    });

}